//----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//----------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using AzureProvider.ssdsClient;
using System.Net;
using BlogEngine.Core;
using AzureProvider.SoupMethods;

public class CategoryMethods : BaseMethods
{

    

    public IEnumerable<Category> GetCategories(string query, out string errMessage)
    {
        return base.GetObjects(query, out errMessage).Cast<Category>();
    }      

    protected override string kind
    {
        get { return "category"; }
    }

    protected override string containerId
    {
        get { return "category"; }
    }

    protected override Entity CreateEntity(object Object)
    {
        Category Category = (Category)Object;
        var entity = new Entity()
        {
            Id = Category.Id.ToString(),
            Kind = kind
        };

        entity.Properties = new Dictionary<string, object>();
        entity.Properties.Add("Title", Category.Title);
        entity.Properties.Add("Description", (Category.Description == null) ? "" : Category.Description);
        //entity.Properties.Add("DateCreated", Category.DateCreated);
        //entity.Properties.Add("DateModified", Category.DateModified);
        entity.Properties.Add("Parent", (Category.Parent == null) ? "" : Category.Parent.ToString());

        return entity;
    }

    public override object CreateObject(Entity Entity)
    {
        Guid? parent = null;
        if (!String.IsNullOrEmpty((string)Entity.Properties.Single(tt => tt.Key == "Parent").Value))
            parent = (new Guid((string)Entity.Properties.Single(tt => tt.Key == "Parent").Value));
        Category myobject = new Category
                        {
                            Id = new Guid(Entity.Id),
                            Title = (string)Entity.Properties.Single(tt => tt.Key == "Title").Value,
                            Description = (string)Entity.Properties.Single(tt => tt.Key == "Description").Value,
                            //DateCreated = (DateTime)Entity.Properties.Single(tt => tt.Key == "DateCreated").Value,
                            //DateModified = (DateTime)Entity.Properties.Single(tt => tt.Key == "DateModified").Value,
                            Parent = parent
                        };
        return myobject;
    }
}
